
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Atienza-Home
 */
public class HighestChanceFrame2 extends javax.swing.JFrame {

    /**
     * Creates new form HighestChanceFrame
     */
    Basket[] bcontain;
    int basketcount;
    int fruitcount;
    String[][] table;
    String[] tableheader;
    ArrayList<Fruit> arrfruits;
    
    String[] fruits;
    int[] quantity;
    
    public HighestChanceFrame2(int basketcount_,int fruitcount_,ArrayList<Fruit> arrfruits_) {
        initComponents();
        setVisible(true);
        setResizable(false);
        
        arrfruits = arrfruits_;
        basketcount = basketcount_;
        fruitcount = fruitcount_;
        table = new String[basketcount][fruitcount+1];
        tableheader = new String[fruitcount+1];
        fruits = new String[fruitcount];
        quantity = new int[fruitcount];
        
        
        
        for(int i=0;i<basketcount;i++){
            table[i][0]="Basket"+(i+1);
        }
         
        for(int i=0,j=0;i<arrfruits.size();i++){
           
            if(arrfruits.get(i).getQuantity()!=0){
                fruits[j]=arrfruits.get(i).getName();
                quantity[j]=arrfruits.get(i).getQuantity();       
                j++;
            }           
        }
        for(int i=0;i<fruits.length;i++){
            for(int j=0;j<fruits.length;j++){
                if(quantity[i]>quantity[j]){
                    String temp = fruits[i];
                    fruits[i] = fruits[j];
                    fruits[j] = temp;
                    int t = quantity[i];
                    quantity[i] = quantity[j];
                    quantity[j] = t;                    
                }
            }
        }
        
        for(int i=0;i<basketcount;i++){
            for(int j=1;j<fruitcount+1;j++){
                table[i][j] = "0";
            }
            
        }
        
        for(int i=1;i<fruitcount+1;i++){
            table[0][i]=String.valueOf(quantity[i-1]);
        }
        boolean basketempty=false;int bemptyi=0;
        for(int i=1;i<basketcount;i++){
            if(Integer.parseInt(table[0][1])!=1){
                table[i][1]=String.valueOf(Integer.parseInt(table[i][1])+1);
                table[0][1]=String.valueOf(Integer.parseInt(table[0][1])-1);
            }            
            else{
                basketempty=true;
                bemptyi=i;
                break;
            }
            
        }
        
        if(basketempty==true){
            for(int i=bemptyi;i<basketcount;i++){
                for(int j=fruitcount;j>=2;j--){
                    if(Integer.parseInt(table[0][j])!=0){
                        table[i][j]=String.valueOf(Integer.parseInt(table[i][1])+1);
                        table[0][j]=String.valueOf(Integer.parseInt(table[0][1])-1);                       
                        break;
                    }                   
                }               
            }            
        }
        int bcount=basketcount; float ftotal=0;
        for(int i=0;i<basketcount;i++){
            int fc=0,ft=0;
            for(int j=1;j<fruitcount+1;j++){             
                if(Integer.parseInt(table[i][j])!=0){
                    if(j==1)
                        fc=Integer.parseInt(table[i][j]);                    
                    ft+=Integer.parseInt(table[i][j]);   
                }
                
                

            }
            
            ftotal+=((float)fc/ft);
        }
        
        float fave = ftotal/bcount;
        
        
        for(int i=0;i<fruits.length;i++){
            tableheader[i+1] = fruits[i];
        }
        
        jLabel2.setText(tableheader[1]+": "+(fave*100)+"%");                      
        
        jTable1.setModel(new javax.swing.table.DefaultTableModel(table,tableheader));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        jLabel1.setText("Highest Chance of Survival");

        jScrollPane1.setBorder(null);
        jScrollPane1.setViewportView(jTable1);

        jLabel2.setFont(new java.awt.Font("Century Gothic", 1, 15)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 153, 51));
        jLabel2.setText("--");

        jButton2.setText("Next>>");

        jLabel3.setText("10");

        jButton3.setText("<<Prev");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jLabel3)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }
  
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration
}
